path = Sys.getenv('PATH')
# [1] "F:\\Rtools\\bin;F:\\Rtools\\gcc-4.6.3\\bin;C:\\Windows\\system32;C

r_arch = Sys.getenv('R_ARCH')
mono_arch_folder = ifelse( r_arch == '/x64', 'x64', 'Win32' )
path = paste(paste('F:\\bin\\mono_built', mono_arch_folder, sep='\\'), path, sep=';')
# path = paste('C:\\Program Files\\Mono-3.0.9\\bin', path, sep=';')
path = paste('c:\\Program Files (x86)\\Mono-3.0.9\\bin', path, sep=';')


Sys.setenv(PATH=path)
Sys.setenv(RCLR='Mono')
# NOTE: if the following is used without a listening MonoDevelop debugger, it crashes RGui...
# Sys.setenv(RCLR_DEBUG='Yep')
# set MONODEVELOP_SDB_TEST=1
# "C:\Program Files (x86)\Xamarin Studio\bin\MonoDevelop.exe"

library(testthat)
library(rClr)

test_package('rClr', 'basic')


clrGetStaticMethods('Rclr.ClrFacade')
setRDotNet(TRUE)
test_package('rClr')

MonoMethod *dest = NULL;
MONO_ARCH_SAVE_REGS;
mono_stack_walk_no_il (get_executing, &dest);
+		dest	0x00000000 {flags=??? iflags=??? token=??? ...}	_MonoMethod *
return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);

mono_walk_stack_full(int (MonoStackFrameInfo *, MonoContext *, void *)* func, MonoContext * start_ctx, _MonoDomain * domain, MonoJitTlsData * jit_tls, MonoLMF * lmf, MonoUnwindOptions unwind_options, void * user_data)  Line 822	C
 	mono-2.0.dll!mono_walk_stack_with_ctx(int (MonoStackFrameInfo *, MonoContext *, void *)* func, MonoContext * start_ctx, MonoUnwindOptions unwind_options, void * user_data)  Line 744 + 0x28 bytes	C
 	mono-2.0.dll!mono_runtime_walk_stack_with_ctx(int (MonoStackFrameInfo *, MonoContext *, void *)* func, MonoContext * start_ctx, MonoUnwindOptions unwind_options, void * user_data)  Line 714 + 0x15 bytes	C
 	mono-2.0.dll!mono_stack_walk_no_il(int (_MonoMethod *, int, int, int, void *)* func, void * user_data)  Line 2274 + 0x18 bytes	C
 	mono-2.0.dll!ves_icall_System_Reflection_Assembly_GetExecutingAssembly()  Line 4896 + 0xe bytes	C


  
# >	mono-2.0.dll!ves_icall_System_Reflection_Assembly_GetExecutingAssembly()  Line 4897 + 0x3 bytes	C
 	# 067cb414()	
 	# 067cb3bc()	
 	# 067cb028()	
 	# 067c0e7d()	
 	# mono-2.0.dll!mono_jit_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 6256 + 0x18 bytes	C
 	# mono-2.0.dll!mono_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 2816 + 0x18 bytes	C
 	# mono-2.0.dll!mono_runtime_class_init_full(MonoVTable * vtable, int raise_exception)  Line 376 + 0x11 bytes	C
 	# mono-2.0.dll!mono_jit_compile_method_inner(_MonoMethod * method, _MonoDomain * target_domain, int opt, _MonoException * * jit_ex)  Line 5796 + 0xb bytes	C
 	# mono-2.0.dll!mono_jit_compile_method_with_opt(_MonoMethod * method, unsigned int opt, _MonoException * * ex)  Line 5867 + 0x15 bytes	C
 	# mono-2.0.dll!mono_jit_compile_method(_MonoMethod * method)  Line 5895 + 0x14 bytes	C
 	# mono-2.0.dll!mono_compile_method(_MonoMethod * method)  Line 581 + 0xc bytes	C
 	# mono-2.0.dll!common_call_trampoline(int * regs, unsigned char * code, _MonoMethod * m, unsigned char * tramp, MonoVTable * vt, void * * vtable_slot, int need_rgctx_tramp)  Line 571 + 0x9 bytes	C
 	# mono-2.0.dll!mono_magic_trampoline(int * regs, unsigned char * code, void * arg, unsigned char * tramp)  Line 694 + 0x1b bytes	C
 	# 06300066()	
 	# 067c0e7d()	
 	# mono-2.0.dll!mono_jit_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 6256 + 0x18 bytes	C
 	# mono-2.0.dll!mono_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 2816 + 0x18 bytes	C
 	# mono-2.0.dll!mono_runtime_class_init_full(MonoVTable * vtable, int raise_exception)  Line 376 + 0x11 bytes	C
 	# mono-2.0.dll!mono_method_to_ir(MonoCompile * cfg, _MonoMethod * method, MonoBasicBlock * start_bblock, MonoBasicBlock * end_bblock, MonoInst * return_var, _GList * dont_inline, MonoInst * * inline_args, unsigned int inline_offset, int is_virtual_call)  Line 9649 + 0xe bytes	C
 	# mono-2.0.dll!mini_method_compile(_MonoMethod * method, unsigned int opts, _MonoDomain * domain, int run_cctors, int compile_aot, int parts)  Line 4866 + 0x21 bytes	C
 	# mono-2.0.dll!mono_jit_compile_method_inner(_MonoMethod * method, _MonoDomain * target_domain, int opt, _MonoException * * jit_ex)  Line 5611 + 0x17 bytes	C
 	# mono-2.0.dll!mono_jit_compile_method_with_opt(_MonoMethod * method, unsigned int opt, _MonoException * * ex)  Line 5867 + 0x15 bytes	C
 	# mono-2.0.dll!mono_jit_compile_method(_MonoMethod * method)  Line 5895 + 0x14 bytes	C
 	# mono-2.0.dll!mono_compile_method(_MonoMethod * method)  Line 581 + 0xc bytes	C
 	# mono-2.0.dll!common_call_trampoline(int * regs, unsigned char * code, _MonoMethod * m, unsigned char * tramp, MonoVTable * vt, void * * vtable_slot, int need_rgctx_tramp)  Line 571 + 0x9 bytes	C
 	# mono-2.0.dll!mono_magic_trampoline(int * regs, unsigned char * code, void * arg, unsigned char * tramp)  Line 694 + 0x1b bytes	C

obj <- clrNew('Rclr.TestObject')
clrGetMethods(obj)
# seems to give the same call stack:

>	mono-2.0.dll!ves_icall_System_Reflection_Assembly_GetExecutingAssembly()  Line 4897 + 0x3 bytes	C
 	119eefac()	
 	119eef54()	
 	119eebc0()	
 	119e0e8d()	
 	mono-2.0.dll!mono_jit_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 6256 + 0x18 bytes	C
 	mono-2.0.dll!mono_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 2816 + 0x18 bytes	C
 	mono-2.0.dll!mono_runtime_class_init_full(MonoVTable * vtable, int raise_exception)  Line 376 + 0x11 bytes	C
 	mono-2.0.dll!mono_jit_compile_method_inner(_MonoMethod * method, _MonoDomain * target_domain, int opt, _MonoException * * jit_ex)  Line 5796 + 0xb bytes	C
 	mono-2.0.dll!mono_jit_compile_method_with_opt(_MonoMethod * method, unsigned int opt, _MonoException * * ex)  Line 5867 + 0x15 bytes	C
 	mono-2.0.dll!mono_jit_compile_method(_MonoMethod * method)  Line 5895 + 0x14 bytes	C
 	mono-2.0.dll!mono_compile_method(_MonoMethod * method)  Line 581 + 0xc bytes	C
 	mono-2.0.dll!common_call_trampoline(int * regs, unsigned char * code, _MonoMethod * m, unsigned char * tramp, MonoVTable * vt, void * * vtable_slot, int need_rgctx_tramp)  Line 571 + 0x9 bytes	C
 	mono-2.0.dll!mono_magic_trampoline(int * regs, unsigned char * code, void * arg, unsigned char * tramp)  Line 694 + 0x1b bytes	C
 	11540066()	
 	119e0e8d()	
 	mono-2.0.dll!mono_jit_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 6256 + 0x18 bytes	C
 	mono-2.0.dll!mono_runtime_invoke(_MonoMethod * method, void * obj, void * * params, MonoObject * * exc)  Line 2816 + 0x18 bytes	C
 	mono-2.0.dll!mono_runtime_class_init_full(MonoVTable * vtable, int raise_exception)  Line 376 + 0x11 bytes	C
 	mono-2.0.dll!mono_method_to_ir(MonoCompile * cfg, _MonoMethod * method, MonoBasicBlock * start_bblock, MonoBasicBlock * end_bblock, MonoInst * return_var, _GList * dont_inline, MonoInst * * inline_args, unsigned int inline_offset, int is_virtual_call)  Line 9649 + 0xe bytes	C
 	mono-2.0.dll!mini_method_compile(_MonoMethod * method, unsigned int opts, _MonoDomain * domain, int run_cctors, int compile_aot, int parts)  Line 4866 + 0x21 bytes	C
 	mono-2.0.dll!mono_jit_compile_method_inner(_MonoMethod * method, _MonoDomain * target_domain, int opt, _MonoException * * jit_ex)  Line 5611 + 0x17 bytes	C
 	mono-2.0.dll!mono_jit_compile_method_with_opt(_MonoMethod * method, unsigned int opt, _MonoException * * ex)  Line 5867 + 0x15 bytes	C
 	mono-2.0.dll!mono_jit_compile_method(_MonoMethod * method)  Line 5895 + 0x14 bytes	C
 	mono-2.0.dll!mono_compile_method(_MonoMethod * method)  Line 581 + 0xc bytes	C
 	mono-2.0.dll!common_call_trampoline(int * regs, unsigned char * code, _MonoMethod * m, unsigned char * tramp, MonoVTable * vt, void * * vtable_slot, int need_rgctx_tramp)  Line 571 + 0x9 bytes	C
 	mono-2.0.dll!mono_magic_trampoline(int * regs, unsigned char * code, void * arg, unsigned char * tramp)  Line 694 + 0x1b bytes	C





# clrInit(FALSE)
# library(testthat)

# # test_package('rClr')

# cTypename <- "Rclr.TestCases"
# expect_that( clrCallStatic(cTypename, "CreateJaggedFloatArray"), equals(matrix(1:15, nrow=3, ncol=5, byrow=TRUE) ));

# # clrLoadAssembly(dllFilename='c:/Users/per202/Documents/Visual Studio 2012/Projects/ReproExtAtt/ReproExtAtt/bin/Debug/ReproExtAtt.exe')

# clrCallStatic('Rclr.TestObject', 'SetRDotNet', TRUE)

# test_package('rClr')
